package fightplan.algorithmen.ko;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

import fightplan.business.AbstractFighter;
import fightplan.business.FighterProvider;

@SuppressWarnings("serial")
public class FighterDojoLabel extends JPanel implements PropertyChangeListener {
	public static final int dojoNameSize = 50;

	private FighterProvider fighterProvider;
	private JLabel fighterLabel;
	private JLabel dojoLabel;

	public FighterDojoLabel(FighterProvider fighterProvider) {
		super(new FlowLayout(FlowLayout.RIGHT, 0, 0));
		this.fighterProvider = fighterProvider;
		setOpaque(false);
		fighterLabel = new JLabel();
		fighterLabel.setBorder(null);
		fighterLabel.setHorizontalAlignment(SwingConstants.LEFT);
		setBorder(BorderFactory.createLineBorder(Color.BLACK));
		fighterLabel.setFont(getFont().deriveFont(10F));
		fighterLabel.setFont(getFont().deriveFont(Font.BOLD));
		dojoLabel = new JLabel();
		fighterLabel.setHorizontalAlignment(SwingConstants.LEFT);
		dojoLabel.setBorder(BorderFactory.createMatteBorder(0, 1, 0, 0, Color.BLACK));
		dojoLabel.setFont(fighterLabel.getFont());
		dojoLabel.setPreferredSize(new Dimension(dojoNameSize, 21));
		add(fighterLabel);
		add(dojoLabel);

		if (fighterProvider != null) {
			updateText();
			fighterProvider.addPropertyChangeListener(this);
		} else {
			fighterLabel.setText("");
			dojoLabel.setText("");
		}
	}

	private void updateText() {
		AbstractFighter fighter = fighterProvider.getFighter();
		if (fighter == null) {
			fighterLabel.setText("");
			dojoLabel.setText("");
		} else {
			fighterLabel.setText(fighter.getName());
			dojoLabel.setText(fighter.getDojo() != null ? fighter.getDojo().getName() : "");
		}
	}

	// PropertyChangeListener

	public void propertyChange(PropertyChangeEvent evt) {
		updateText();
	}

}
